import React, { Component } from "react";
class ErrorBoundary extends Component {
  constructor(props) {
    super(props);
    this.state = { hasError: false };
  }
  static getDerivedStateFromError(error) {
    return { hasError: true }; // 更新状态，使下游组件不再渲染
  }
  componentDidCatch(error, info) {
    console.error("Error caught by Error Boundary:", error, info);
  }
  render() {
    if (this.state.hasError) {
      return <h1>Something went wrong.</h1>; // 自定义错误界面
    }
    return this.props.children; // 正常渲染子组件
  }
}
export default ErrorBoundary;
